""" 菜单管理视图函数模块"""
from flask import request

from air_web.web_flask.bll.menu_management_bll import MenuManagementBll
from air_web.web_flask.common.validator import (
    T_INT,
    T_LIST,
    T_STR,
    Fields,
    with_validator,
)
from air_web.web_flask.views.base_page import BasePage


class MenuManagementPageValidationFields:
    add_menu = [
        Fields("title", T_STR, must=True),
        Fields("jump", T_STR, must=True),
        Fields("report_url", T_STR, must=True),
        Fields("priority", T_INT, must=True),
        Fields("is_publish", T_INT, must=True),
    ]

    get_menu = [
        Fields("is_publish", T_INT, must=False),
        Fields("pageSize", T_INT, must=True),
        Fields("pageNum", T_INT, must=True),
    ]

    delete_menu = [Fields("id_list", T_LIST, must=True)]

    update_menu = [
        Fields("id", T_INT, must=True),
        Fields("title", T_STR, must=True),
        Fields("priority", T_INT, must=True),
        Fields("jump", T_STR, must=True),
        Fields("report_url", T_STR, must=True),
        Fields("is_publish", T_INT, must=True),
    ]


class MenuManagementPage(BasePage):
    """总览页面视图函数类"""

    @classmethod
    @with_validator(request, MenuManagementPageValidationFields.add_menu)
    def add_menu(cls):
        validation_data = request.validation_data
        result = MenuManagementBll().add_menu(validation_data)
        return cls.return_common_func(result, custom_response=True)

    @classmethod
    @with_validator(request, MenuManagementPageValidationFields.get_menu)
    def get_menu(cls):
        validation_data = request.validation_data
        result = MenuManagementBll().get_menu(validation_data)
        return cls.return_common_func(result, paging=True)

    @classmethod
    @with_validator(request, MenuManagementPageValidationFields.delete_menu)
    def delete_menu(cls):
        validation_data = request.validation_data
        result = MenuManagementBll().delete_menu(validation_data)
        return cls.return_common_func(result, custom_response=True)

    @classmethod
    @with_validator(request, MenuManagementPageValidationFields.update_menu)
    def update_menu(cls):
        validation_data = request.validation_data
        result = MenuManagementBll().update_menu(validation_data)
        return cls.return_common_func(result, custom_response=True)

    @classmethod
    def test(cls):
        print(request.json)
        print("test_data")
        result = request.json
        import json

        return json.dumps(result)
